"""
pyx属于python的超集, 实际以python为主

导入属于比较麻烦的一部分, 首先需要明确它属于动态部分
实际导入会安装python的逻辑寻找
from example_cython_extension.includes cimport example_month_hall

目录以及Extension名称要对上, 具体见build.py
Lib/site-package    
  - example_cython_extension
    - includes
      - example_month_hall
"""

"""
# 这部分用于extra
# 这部分需要将build.py point1的注释取消
from includes.example_month_hall cimport (
    monty_hall_simulation as py_monty_hall_simulation,
    random_int,
    MResult,
    init
)
"""
from example_cython_extension.includes.example_month_hall cimport (
    monty_hall_simulation as py_monty_hall_simulation,
    random_int,
    MResult,
    init
)

# c/tests/math/test_cmath.py test_add
# 内部使用随机数等，需要在c侧初始化或者提供初始化方法，具体原因查看生成的c代码
init()

def monty_hall_simulation(num:int)->tuple[int,int]:
    cdef MResult res = py_monty_hall_simulation(num)
    tmp = (res.win_by_sticking,res.win_by_switching,)
    return tmp

cdef MResult c_monty_hall_simulation_cython(int num):
    # 通过明确的类型定义可以有效地降低时间
    cdef MResult res
    res.win_by_switching = 0
    res.win_by_sticking = 0
    cdef int win_by_sticking = 0
    cdef int win_by_switching = 0
    cdef int i = 0
    # cdef 不能在不稳定情况下使用，比如循环等
    while (i<num):
        i+=1
        car_position:int = random_int(0, 3)
        contestant_choice:int= random_int(0, 3)
        doors= [0]*2
        d_i:int = 0
        for j in range(3) :
            if (j != car_position and  j != contestant_choice) :
                doors[d_i]= j
                d_i+=1
        
        host_open:int = doors[random_int(0, d_i)]
        if (contestant_choice == car_position) :
            win_by_sticking+=1
        else :
            switch_choice:int
            for j in range(3) :
                if (j != contestant_choice and j != host_open) :
                    switch_choice = j
                    break
            if (switch_choice == car_position):
                win_by_switching+=1
    res.win_by_switching = win_by_switching
    res.win_by_sticking = win_by_sticking
    return res


def monty_hall_simulation_cython(num :int)->tuple[int,int]:
    cdef MResult res = c_monty_hall_simulation_cython(num)
    tmp = (res.win_by_sticking,res.win_by_switching,)
    return tmp

